#include <stdio.h>
#include <string.h>
#define maxn 110

char dna[maxn * 2], mindna[maxn], subdna[maxn];

int main() {
  int k, i, j, len;
  scanf("%d", &k);
  while (k--) {
    memset(dna, '\0', sizeof(dna));
    memset(mindna, '\0', sizeof(mindna));
    memset(subdna, '\0', sizeof(subdna));
    scanf("%s", dna);
    len = strlen(dna);
    strncat(mindna, dna, len);    
    strncat(dna, dna, len);
    for (i = 0; i < len; i++) {
      for (j = 0; j < len; j++) {
        subdna[j] = dna[j + i];
      }
      
      if (strcmp(subdna, mindna) < 0) {
        strcpy(mindna, subdna);
      }
    }
    
    puts(mindna);
  }
  
  return 0;
}
